Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
Axios + Express | Empty req.body (solved)

// START EIDT:

Solution was (thanks to mr. Hawkeye @robertklep) the name "body" in my post request in axios on the client side. Apparently you cannot name the body in axios post request anything but data, even though it's a reference passed from a parameter or similar.

// END EIDT:

I'm currently building an application with React as the frontend and Express as backend.

For API calls I'm using axios. My APIhas been working fine, but all of the sudden with no obvious change my post request body is not being sent to the backend.

In my index.js I had the following:

app.use(bodyParser.json());

but also tried with

app.use(express.json())

Client side function calling my post request:

 const checkForExistingUserData = async () => {
        if (user) {
            const body = {
                caseId: user['caseId']
            }

            await postRequest({request: body}, commonApiLocations["userDataLookUp"])
    }
}

My client side call is being call this way:

export function postRequest(body, url) {

        console.log(body) // Logs correct body with my data inside

        const config = {
        method: 'post',
        url: masterEndPoints['expressEndPoint'] + url,
        headers: {
            'Content-Type': 'application/json',
        },
        body // should be called "data" (see edit in top)
    };
    return axios(config);
}

Express route endpoint function:

route.post('/api/users/checkExistingUserData', async (req, res) => {

    try {
        const request = req.body.request;

        console.log("request: ", request); // Gives undefined

        if (request['caseId']) {
            let result = await checkIfExist(tableNames['TableName'], {'caseId': request.caseId})
            res.status(200).send(result);
        } else {
            res.status(200).send(false)
        }
    } catch (error) {
        res.status(500).send('En error occurred on the server')
    }
});

When I console.log on on the client side I see my request with the data that I expect to see.

When I console.log on the server I get an undefined.

When I console.log req.body I get "{}"

Any reasons for this error? I'm completely dumbfounded by this.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda